let path = require('path')
let express = require('express')
let session = require('express-session')
let MongoStore = require('connect-mongo')(session)
let flash = require('connect-flash')
let config = require('config-lite')
let routes = require('./routes')
let pkg = require('./package')

let app = express()

// views
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')

// headers
app.set('x-powered-by', false)

// static public resource
app.use(express.static(path.join(__dirname, 'public')))

// session middleware
app.use(session({
  name: config.session.key,
  secret: config.session.secret,
  cookie: {
    maxAge: config.session.maxAge
  },
  store: new MongoStore({
    url: config.mongodb
  })
}))

// connect flash
app.use(flash())

// routers
routes(app)

// listen and start
app.listen(config.port, () => {
  console.log(`${pkg.name} listening on port ${config.port}`)
})
